package com.yht.org.service.impl;

import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import com.yht.basic.util.QueryResult;
import com.yht.org.domain.Department;
import com.yht.org.mapper.DepartmentMapper;
import com.yht.org.query.DepartmentQuery;
import com.yht.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.insert(department);
        //直接调用下面的update方法
        this.update(department);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        //处理部门所属路径字段
        String selfDirPath = "";
        if(department.getParent() == null){
            //没有父部们，其路径就是斜杠+自己的id
            selfDirPath = "/" + department.getId();
        }else {
            //如果有父部们，就调用根据id查询的方法查出父部们
            Department parent = departmentMapper.selectById(department.getParent().getId());
            //自己的dirpath就是父部们的dirpath+/+自己的id
            selfDirPath = parent.getDirPath() + "/" + department.getId();
        }

        department.setDirPath(selfDirPath);
        //更新
        departmentMapper.update(department);
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.selectById(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public QueryResult<Department> findByDepartmentQuery(DepartmentQuery departmentQuery) {
        Integer total = departmentMapper.selectCount(departmentQuery);
        if(total == 0){
            return new QueryResult<>();
        }
        List<Department> departments = departmentMapper.selectByDepartQuery(departmentQuery);
        QueryResult<Department> result = new QueryResult<>(total, departments);
        return result;
    }

    @Override
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);
    }
    /*嵌套循环 性能不是很好*/
   /* @Override
    public List<Department> getTree() {
        //查询出所有部门
        List<Department> departments = departmentMapper.selectAll();
        //生明好要返回的集合，装顶级部门
        ArrayList<Department> result = new ArrayList<>();
        //2、遍历所有，如果是顶级部门就放到result中
        for (Department department : departments) {
            if(department.getParent_id() == null){
                result.add(department);
            }else {//3、如果不是顶级部门就寻找自己的父部们
                for (Department parent : departments) {
                    if(department.getParent_id() == parent.getId()){
                        //把自己装到父部们的children中
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break;//结束内层循环
                    }
                }                
            }
        }
        return result;
    }*/
    @Override
    public List<Department> getTree() {
        //查询出所有部门
        List<Department> departments = departmentMapper.selectAll();
        //生明一个map，key是每个department对象的id，value是该对象
        Map<Long, Department> map = departments.stream().collect(Collectors.toMap(Department::getId, Function.identity()));
        //声明一个list
        ArrayList<Department> result = new ArrayList<>();
        //2、遍历所有，如果是顶级部门就放到result中
        for (Department department : departments) {
            if(department.getParent_id() == null){
                //添加顶级部门
                result.add(department);
            }else {//3、如果不是顶级部门就寻找自己的父部们
                Long pid = department.getParent_id();
                Department parent = map.get(pid);
                if(parent != null){
                    List<Department> children = parent.getChildren();
                    children.add(department);
                }
            }
        }
        return result;
    }
}
